const db=require('../utils/mysql')
const { v4: uuidv4 } = require('uuid');
module.exports={
    personAdd:async (ctx,next)=>{
        let query=ctx.request.body
        let personId=uuidv4()
        let result= await new Promise((resolve,reject)=>{
            const sql=`INSERT INTO user (personId,username,password,nickname,headphoto,phone,identity,sex,type,entrytime,status) 
            values ('${personId}','${query.username}','${query.password}','${query.nickname}','${query.headphoto}','${query.phone}','repairer',
            '${query.sex=='男'?1:0}','${query.type}','${query.entrytime}','${query.status=='在职'?1:0}')`
            db(sql,(err,data)=>{
                if(err) reject(err);
                resolve({
                    msg:'人员添加成功',
                    status:200
                })
            })
        })
        ctx.body=result
    },
    personEdit:async (ctx,next)=>{
        let query=ctx.request.body
        console.log(query)
        let result= await new Promise((resolve,reject)=>{
            const sql=`update user set 
            username='${query.username}',password='${query.password}',nickname='${query.nickname}',entrytime='${query.entrytime}',
            headphoto='${query.headphoto}',type='${query.type}',sex='${query.sex=='男'?1:0}',phone='${query.phone}',status='${query.status=='在职'?1:0}'
            where personId='${query.personId}'`
            db(sql,(err,data)=>{
                if(err) reject(err);
                resolve({
                    msg:'人员修改成功',
                    status:200
                })
            })
        })
        ctx.body=result
    },
    personDelete:async (ctx,next)=>{
        let query=ctx.request.query
        let result= await new Promise((resolve,reject)=>{
            const sql=`delete from user where personId='${query.personId}'`
            db(sql,(err,data)=>{
                if(err) reject(err);
                resolve({
                    msg:'人员删除成功',
                    status:200
                })
            })
        })
        ctx.body=result
    },
    getpersonList:async (ctx,next)=>{
        let result= await new Promise((resolve,reject)=>{
            const sql=`select personId,nickname,type,phone,status from user where identity='repairer'`
            db(sql,(err,data)=>{
                if(err) reject(err);
                resolve({
                    msg:'人员列表获取成功',
                    status:200,
                    data:data
                })
            })
        })
        ctx.body=result

    },
    getNowPersonList:async (ctx,next)=>{
        let result= await new Promise((resolve,reject)=>{
            const sql1=`select * from repairer_type` //去工种表查工种
            db(sql1,(err,data)=>{
                if(err) reject(err);
                if(data.length==0) resolve(data);
                let array=[]//维修人员的数组
                data.forEach(item=>{//按照工种去人员表查询维修人员
                    const sql=`select personId as value,nickname as text from user where type='${item.name}'`
                    db(sql,(err,data)=>{
                        if(err) reject(err);
                        if (data.length>0){//进行格式化处理
                            array.push({
                                text:item.name,
                                value:item.typeId,
                                children:data
                            })
                        }else {
                            array.push({
                                text:item.name,
                                value:item.typeId
                            })
                        }
                    })
                })
                setTimeout(()=>{//延迟返回
                    resolve({
                        msg:'维修人员列表获取成功',
                        status:200,
                        data:array
                    })
                },1000)
            })
        })
        ctx.body=result
    },
    getPersonInfo:async (ctx,next)=>{
        let query=ctx.request.query
        let result= await new Promise((resolve,reject)=>{
            const sql=`select * from user where personId='${query.personId}'`
            db(sql,(err,data)=>{
                if(err) reject(err);
                resolve({
                    msg:'人员详情获取成功',
                    status:200,
                    data:data
                })
            })
        })
        ctx.body=result

    },
}

